<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车 - GameHub | 学号-姓名-实验4</title>
    <style>
        /* 使用与主页面相同的CSS变量和基础样式 */
        :root {
            --primary-color: #6c5ce7;
            --secondary-color: #a29bfe;
            --dark-color: #2d3436;
            --light-color: #f5f6fa;
            --success-color: #00b894;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background-color: var(--light-color);
            color: var(--dark-color);
        }
        
        header {
            background-color: var(--primary-color);
            color: white;
            padding: 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .logo {
            font-size: 1.8rem;
            font-weight: bold;
        }
        
        nav ul {
            display: flex;
            list-style: none;
        }
        
        nav ul li {
            margin-left: 1.5rem;
        }
        
        nav ul li a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        nav ul li a:hover {
            color: var(--secondary-color);
        }
        
        .user-actions a {
            color: white;
            text-decoration: none;
            margin-left: 1rem;
        }

        footer {
        background-color: var(--dark-color);
        color: white;
        padding: 2rem;
        margin-top: 2rem;
    }
    
    .footer-content {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 2rem;
        max-width: 1200px;
        margin: 0 auto;
    }
    
    .footer-section h3 {
        margin-bottom: 1rem;
        font-size: 1.2rem;
    }
    
    .footer-section ul {
        list-style: none;
    }
    
    .footer-section ul li {
        margin-bottom: 0.5rem;
    }
    
    .footer-section ul li a {
        color: #ddd;
        text-decoration: none;
        transition: color 0.3s ease;
    }
    
    .footer-section ul li a:hover {
        color: white;
    }
    
    .copyright {
        text-align: center;
        margin-top: 2rem;
        padding-top: 1rem;
        border-top: 1px solid rgba(255, 255, 255, 0.1);
        max-width: 1200px;
        margin-left: auto;
        margin-right: auto;
    }

    /* 响应式设计 - 页脚 */
    @media (max-width: 768px) {
        .footer-content {
            grid-template-columns: 1fr 1fr;
        }
    }

        
        /* 购物车特定样式 */
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 1rem;
        }
        
        .cart-title {
            font-size: 2rem;
            margin: 2rem 0;
            color: var(--dark-color);
            text-align: center;
        }
        
        .cart-container {
            display: flex;
            flex-wrap: wrap;
            gap: 2rem;
        }
        
        .cart-items {
            flex: 2;
            background-color: white;
            border-radius: 8px;
            padding: 1.5rem;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }
        
        .cart-summary {
            flex: 1;
            background-color: white;
            border-radius: 8px;
            padding: 1.5rem;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            height: fit-content;
        }
        
        .cart-item {
            display: flex;
            border-bottom: 1px solid #eee;
            padding: 1rem 0;
            align-items: center;
        }
        
        .cart-item:last-child {
            border-bottom: none;
        }
        
        .cart-item-image {
            width: 120px;
            height: 80px;
            margin-right: 1rem;
        }
        
        .cart-item-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 4px;
        }
        
        .cart-item-details {
            flex: 1;
        }
        
        .cart-item-title {
            font-weight: 600;
            margin-bottom: 0.5rem;
        }
        
        .cart-item-price {
            color: var(--primary-color);
            font-weight: bold;
        }
        
        .cart-item-actions {
            display: flex;
            align-items: center;
        }
        
        .quantity-control {
            display: flex;
            align-items: center;
            margin-right: 1rem;
        }
        
        .quantity-btn {
            width: 30px;
            height: 30px;
            background-color: var(--light-color);
            border: none;
            font-size: 1rem;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .quantity-input {
            width: 50px;
            height: 30px;
            text-align: center;
            margin: 0 5px;
            border: 1px solid #ddd;
        }
        
        .remove-btn {
            background: none;
            border: none;
            color: #ff7675;
            cursor: pointer;
            font-size: 1.2rem;
        }
        
        .summary-title {
            font-size: 1.5rem;
            margin-bottom: 1.5rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid #eee;
        }
        
        .summary-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 1rem;
        }
        
        .summary-total {
            font-weight: bold;
            font-size: 1.2rem;
            margin: 1.5rem 0;
            padding-top: 1rem;
            border-top: 1px solid #eee;
        }
        
        .btn {
            padding: 0.8rem 1.5rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            width: 100%;
            text-align: center;
            display: block;
            text-decoration: none;
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            color: white;
            margin-bottom: 1rem;
        }
        
        .btn-primary:hover {
            background-color: #5649c0;
        }
        
        .btn-secondary {
            background-color: var(--dark-color);
            color: white;
        }
        
        .btn-secondary:hover {
            background-color: #1a1e1f;
        }
        
        .empty-cart {
            text-align: center;
            padding: 3rem 0;
        }
        
        .empty-cart-icon {
            font-size: 5rem;
            color: #ddd;
            margin-bottom: 1rem;
        }
        
        .empty-cart-message {
            font-size: 1.2rem;
            margin-bottom: 1.5rem;
            color: var(--dark-color);
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .cart-container {
                flex-direction: column;
            }
            
            .cart-item {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .cart-item-image {
                margin-bottom: 1rem;
            }
            
            .cart-item-actions {
                margin-top: 1rem;
                width: 100%;
                justify-content: space-between;
            }
        }

        .empty-cart {
            text-align: center;
            padding: 3rem 0;
        }
        
        .empty-cart-icon {
            font-size: 5rem;
            color: #ddd;
            margin-bottom: 1rem;
        }
        
        .empty-cart-message {
            font-size: 1.2rem;
            margin-bottom: 1.5rem;
            color: var(--dark-color);
        }

    </style>
</head>
<body>
    <!-- 头部导航 - 与主页面一致 -->
    <header>
        <div class="logo">GameHub</div>
        <nav>
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="#">游戏商城</a></li>
                <li><a href="#">特惠活动</a></li>
                <li><a href="#">新游预告</a></li>
                <li><a href="#">社区论坛</a></li>
            </ul>
        </nav>
        <div class="user-actions">
            <a href="#" id="login-btn">登录</a>
            <a href="#" id="register-btn">注册</a>
            <a href="cart.html"><i class="fas fa-shopping-cart"></i> 购物车</a>
        </div>
    </header>

    <!-- 购物车内容 -->
    <div class="container">
        <h1 class="cart-title">我的购物车</h1>
        
        <div class="cart-container">
            <div class="cart-items">
                <!-- 购物车商品1 -->
                <div class="cart-item">
                    <div class="cart-item-image">
                        <img src="https://via.placeholder.com/120x80/6c5ce7/ffffff?text=游戏1" alt="游戏1">
                    </div>
                    <div class="cart-item-details">
                        <h3 class="cart-item-title">幻想世界：失落王国</h3>
                        <div class="cart-item-price">¥199</div>
                    </div>
                    <div class="cart-item-actions">
                        <div class="quantity-control">
                            <button class="quantity-btn">-</button>
                            <input type="number" class="quantity-input" value="1" min="1">
                            <button class="quantity-btn">+</button>
                        </div>
                        <button class="remove-btn">×</button>
                    </div>
                </div>
                
                <!-- 购物车商品2 -->
                <div class="cart-item">
                    <div class="cart-item-image">
                        <img src="https://via.placeholder.com/120x80/a29bfe/ffffff?text=游戏2" alt="游戏2">
                    </div>
                    <div class="cart-item-details">
                        <h3 class="cart-item-title">星际争霸：新纪元</h3>
                        <div class="cart-item-price">¥249</div>
                    </div>
                    <div class="cart-item-actions">
                        <div class="quantity-control">
                            <button class="quantity-btn">-</button>
                            <input type="number" class="quantity-input" value="1" min="1">
                            <button class="quantity-btn">+</button>
                        </div>
                        <button class="remove-btn">×</button>
                    </div>
                </div>
                
                <!-- 购物车商品3 -->
                <div class="cart-item">
                    <div class="cart-item-image">
                        <img src="https://via.placeholder.com/120x80/00b894/ffffff?text=游戏3" alt="游戏3">
                    </div>
                    <div class="cart-item-details">
                        <h3 class="cart-item-title">极限竞速：地平线</h3>
                        <div class="cart-item-price">¥179</div>
                    </div>
                    <div class="cart-item-actions">
                        <div class="quantity-control">
                            <button class="quantity-btn">-</button>
                            <input type="number" class="quantity-input" value="1" min="1">
                            <button class="quantity-btn">+</button>
                        </div>
                        <button class="remove-btn">×</button>
                    </div>
                </div>
            </div>
            
            <div class="cart-summary">
                <h3 class="summary-title">订单摘要</h3>
                <div class="summary-row">
                    <span>商品总价</span>
                    <span>¥627</span>
                </div>
                <div class="summary-row">
                    <span>折扣优惠</span>
                    <span>-¥50</span>
                </div>
                <div class="summary-row">
                    <span>运费</span>
                    <span>¥0</span>
                </div>
                <div class="summary-total">
                    <span>应付总额</span>
                    <span>¥577</span>
                </div>
                <a href="ddddd.html" class="btn btn-primary">去结算</a>
                <a href="index.html" class="btn btn-secondary">继续购物</a>
            </div>
        </div>
    </div>

    <!-- 页脚 - 与主页面一致 -->
    <footer>
        <div class="footer-content">
            <div class="footer-section">
                <h3>关于我们</h3>
                <ul>
                    <li><a href="#">公司简介</a></li>
                    <li><a href="#">团队介绍</a></li>
                    <li><a href="#">联系方式</a></li>
                    <li><a href="#">加入我们</a></li>
                </ul>
            </div>
            <div class="footer-section">
                <h3>客户服务</h3>
                <ul>
                    <li><a href="#">帮助中心</a></li>
                    <li><a href="#">支付方式</a></li>
                    <li><a href="#">配送方式</a></li>
                    <li><a href="#">退换政策</a></li>
                </ul>
            </div>
            <div class="footer-section">
                <h3>法律信息</h3>
                <ul>
                    <li><a href="#">用户协议</a></li>
                    <li><a href="#">隐私政策</a></li>
                    <li><a href="#">版权声明</a></li>
                    <li><a href="#">知识产权</a></li>
                </ul>
            </div>
            <div class="footer-section">
                <h3>关注我们</h3>
                <ul>
                    <li><a href="#">微信公众号</a></li>
                    <li><a href="#">微博</a></li>
                    <li><a href="#">B站</a></li>
                    <li><a href="#">抖音</a></li>
                </ul>
            </div>
        </div>
        <div class="copyright">
            <p>© 2023 GameHub游戏购物平台. 版权所有. 学号：23215220331 姓名：黄毅</p>
        </div>
    </footer>

    <!-- JavaScript -->
    <script>
        // 数量增减功能
        document.querySelectorAll('.quantity-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const input = this.parentElement.querySelector('.quantity-input');
                let value = parseInt(input.value);
                
                if (this.textContent === '+') {
                    input.value = value + 1;
                } else {
                    if (value > 1) {
                        input.value = value - 1;
                    }
                }
                
                // 这里应该有更新总价的逻辑
                updateCartTotal();
            });
        });
        
        // 数量输入框变化
        document.querySelectorAll('.quantity-input').forEach(input => {
            input.addEventListener('change', function() {
                if (this.value < 1) {
                    this.value = 1;
                }
                
                // 这里应该有更新总价的逻辑
                updateCartTotal();
            });
        });
        
        // 删除商品
        document.querySelectorAll('.remove-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const cartItem = this.closest('.cart-item');
                cartItem.remove();
                
                // 检查购物车是否为空
                if (document.querySelectorAll('.cart-item').length === 0) {
                    showEmptyCart();
                } else {
                    updateCartTotal();
                }
            });
        });
        
        // 更新购物车总价
        function updateCartTotal() {
            // 这里应该有计算总价的逻辑
            console.log('更新购物车总价');
        }
        
        // 显示购物车为空
        function showEmptyCart() {
            const cartItems = document.querySelector('.cart-items');
            cartItems.innerHTML = `
                <div class="empty-cart">
                    <div class="empty-cart-icon">🛒</div>
                    <div class="empty-cart-message">您的购物车是空的</div>
                    <a href="index.html" class="btn btn-primary">去逛逛</a>
                </div>
            `;
            
            // 隐藏订单摘要
            document.querySelector('.cart-summary').style.display = 'none';
        }


        document.addEventListener('DOMContentLoaded', function() {
            // 获取购物车数据
            const cart = JSON.parse(localStorage.getItem('gamehub-cart')) || [];
            const cartItemsContainer = document.querySelector('.cart-items');
            const cartSummary = document.querySelector('.cart-summary');
            
            // 渲染购物车商品
            function renderCart() {
                if (cart.length === 0) {
                    cartItemsContainer.innerHTML = `
                        <div class="empty-cart">
                            <div class="empty-cart-icon">🛒</div>
                            <div class="empty-cart-message">您的购物车是空的</div>
                            <a href="index.html" class="btn btn-primary">去逛逛</a>
                        </div>
                    `;
                    cartSummary.style.display = 'none';
                    return;
                }
                
                // 计算总价
                let subtotal = 0;
                
                // 生成商品列表HTML
                let itemsHTML = '';
                cart.forEach(item => {
                    subtotal += item.price * item.quantity;
                    
                    itemsHTML += `
                        <div class="cart-item" data-id="${item.id}">
                            <div class="cart-item-image">
                                <img src="${item.image}" alt="${item.title}">
                            </div>
                            <div class="cart-item-details">
                                <h3 class="cart-item-title">${item.title}</h3>
                                <div class="cart-item-price">¥${item.price.toFixed(2)}</div>
                            </div>
                            <div class="cart-item-actions">
                                <div class="quantity-control">
                                    <button class="quantity-btn minus">-</button>
                                    <input type="number" class="quantity-input" value="${item.quantity}" min="1">
                                    <button class="quantity-btn plus">+</button>
                                </div>
                                <button class="remove-btn">×</button>
                            </div>
                        </div>
                    `;
                });
                
                cartItemsContainer.innerHTML = itemsHTML;
                
                // 更新订单摘要
                const discount = subtotal > 300 ? 50 : 0;
                const total = subtotal - discount;
                
                cartSummary.innerHTML = `
                    <h3 class="summary-title">订单摘要</h3>
                    <div class="summary-row">
                        <span>商品总价</span>
                        <span>¥${subtotal.toFixed(2)}</span>
                    </div>
                    <div class="summary-row">
                        <span>折扣优惠</span>
                        <span>-¥${discount.toFixed(2)}</span>
                    </div>
                    <div class="summary-row">
                        <span>运费</span>
                        <span>¥0.00</span>
                    </div>
                    <div class="summary-total">
                        <span>应付总额</span>
                        <span>¥${total.toFixed(2)}</span>
                    </div>
                    <a href="ddddd.html" class="btn btn-primary checkout-btn">去结算</a>
                    <a href="index.html" class="btn btn-secondary">继续购物</a>
                `;
                
                // 添加事件监听
                addCartEventListeners();
            }
            
            // 添加购物车操作事件
            function addCartEventListeners() {
                // 数量减少
                document.querySelectorAll('.quantity-btn.minus').forEach(btn => {
                    btn.addEventListener('click', function() {
                        const input = this.nextElementSibling;
                        if (parseInt(input.value) > 1) {
                            input.value = parseInt(input.value) - 1;
                            updateCartItem(this);
                        }
                    });
                });
                
                // 数量增加
                document.querySelectorAll('.quantity-btn.plus').forEach(btn => {
                    btn.addEventListener('click', function() {
                        const input = this.previousElementSibling;
                        input.value = parseInt(input.value) + 1;
                        updateCartItem(this);
                    });
                });
                
                // 手动输入数量
                document.querySelectorAll('.quantity-input').forEach(input => {
                    input.addEventListener('change', function() {
                        if (this.value < 1) this.value = 1;
                        updateCartItem(this);
                    });
                });
                
                // 删除商品
                document.querySelectorAll('.remove-btn').forEach(btn => {
                    btn.addEventListener('click', function() {
                        const itemId = this.closest('.cart-item').dataset.id;
                        removeCartItem(itemId);
                    });
                });
            }
            
            // 更新购物车商品
            function updateCartItem(element) {
                const itemId = element.closest('.cart-item').dataset.id;
                const newQuantity = parseInt(element.closest('.quantity-control').querySelector('.quantity-input').value);
                
                let cart = JSON.parse(localStorage.getItem('gamehub-cart')) || [];
                const item = cart.find(item => item.id === itemId);
                
                if (item) {
                    item.quantity = newQuantity;
                    localStorage.setItem('gamehub-cart', JSON.stringify(cart));
                    renderCart();
                }
            }
            
            // 移除购物车商品
            function removeCartItem(itemId) {
                let cart = JSON.parse(localStorage.getItem('gamehub-cart')) || [];
                cart = cart.filter(item => item.id !== itemId);
                localStorage.setItem('gamehub-cart', JSON.stringify(cart));
                renderCart();
            }
            
            // 初始渲染
            renderCart();
            
            // 更新购物车链接显示数量
            const cartLinks = document.querySelectorAll('.user-actions a[href="cart.html"]');
            cartLinks.forEach(link => {
                link.innerHTML = '<i class="fas fa-shopping-cart"></i> 购物车 <span class="cart-count"></span>';
                updateCartCount();
            });
            
            // 更新购物车数量显示
            function updateCartCount() {
                const cart = JSON.parse(localStorage.getItem('gamehub-cart')) || [];
                const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
                const cartCounts = document.querySelectorAll('.cart-count');
                
                cartCounts.forEach(count => {
                    count.textContent = totalItems;
                    count.style.display = totalItems > 0 ? 'inline-block' : 'none';
                });
            }
        });
    </script>
</body>
</html>